//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.CountingExecutionEntity;
import org.activiti.engine.impl.persistence.entity.data.DataManager;
import org.activiti.engine.impl.persistence.entity.data.IdentityLinkDataManager;

public class IdentityLinkEntityManagerImpl extends AbstractEntityManager<IdentityLinkEntity> implements IdentityLinkEntityManager {
  protected IdentityLinkDataManager identityLinkDataManager;

  public IdentityLinkEntityManagerImpl(ProcessEngineConfigurationImpl processEngineConfiguration, IdentityLinkDataManager identityLinkDataManager) {
    super(processEngineConfiguration);
    this.identityLinkDataManager = identityLinkDataManager;
  }

  protected DataManager<IdentityLinkEntity> getDataManager() {
    return this.identityLinkDataManager;
  }

  public void insert(IdentityLinkEntity entity, boolean fireCreateEvent) {
    super.insert(entity, fireCreateEvent);
    this.getHistoryManager().recordIdentityLinkCreated(entity);
    if (entity.getProcessInstanceId() != null && this.isExecutionRelatedEntityCountEnabledGlobally()) {
      CountingExecutionEntity executionEntity = (CountingExecutionEntity)this.getExecutionEntityManager().findById(entity.getProcessInstanceId());
      if (this.isExecutionRelatedEntityCountEnabled(executionEntity)) {
        executionEntity.setIdentityLinkCount(executionEntity.getIdentityLinkCount() + 1);
      }
    }

  }

  public void deleteIdentityLink(IdentityLinkEntity identityLink, boolean cascadeHistory) {
    this.delete(identityLink, false);
    if (cascadeHistory) {
      this.getHistoryManager().deleteHistoricIdentityLink(identityLink.getId());
    }

    if (identityLink.getProcessInstanceId() != null && this.isExecutionRelatedEntityCountEnabledGlobally()) {
      CountingExecutionEntity executionEntity = (CountingExecutionEntity)this.getExecutionEntityManager().findById(identityLink.getProcessInstanceId());
      if (this.isExecutionRelatedEntityCountEnabled(executionEntity)) {
        executionEntity.setIdentityLinkCount(executionEntity.getIdentityLinkCount() - 1);
      }
    }

    if (this.getEventDispatcher().isEnabled()) {
      this.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_DELETED, identityLink));
    }

  }

  public List<IdentityLinkEntity> findIdentityLinksByTaskId(String taskId) {
    return this.identityLinkDataManager.findIdentityLinksByTaskId(taskId);
  }

  public List<IdentityLinkEntity> findIdentityLinksByProcessInstanceId(String processInstanceId) {
    return this.identityLinkDataManager.findIdentityLinksByProcessInstanceId(processInstanceId);
  }

  public List<IdentityLinkEntity> findIdentityLinksByProcessDefinitionId(String processDefinitionId) {
    return this.identityLinkDataManager.findIdentityLinksByProcessDefinitionId(processDefinitionId);
  }

  public List<IdentityLinkEntity> findIdentityLinkByTaskUserGroupAndType(String taskId, String userId, String groupId, String type) {
    return this.identityLinkDataManager.findIdentityLinkByTaskUserGroupAndType(taskId, userId, groupId, type);
  }

  public List<IdentityLinkEntity> findIdentityLinkByProcessInstanceUserGroupAndType(String processInstanceId, String userId, String groupId, String type) {
    return this.identityLinkDataManager.findIdentityLinkByProcessInstanceUserGroupAndType(processInstanceId, userId, groupId, type);
  }

  public List<IdentityLinkEntity> findIdentityLinkByProcessDefinitionUserAndGroup(String processDefinitionId, String userId, String groupId) {
    return this.identityLinkDataManager.findIdentityLinkByProcessDefinitionUserAndGroup(processDefinitionId, userId, groupId);
  }

  @Override
  public List<IdentityLinkEntity> findIdentityLinkByOutsideId(String taskId, String outsideId,
      String userId) {
    return null;
  }

  @Override
  public void deleteIdentityLinksByOutsideIdTaskId(String outsideId, String taskId, String userId) {

  }

  public IdentityLinkEntity addIdentityLink(ExecutionEntity executionEntity, String userId, String groupId, String type) {
    IdentityLinkEntity identityLinkEntity = (IdentityLinkEntity)this.identityLinkDataManager.create();
    executionEntity.getIdentityLinks().add(identityLinkEntity);
    identityLinkEntity.setProcessInstance(executionEntity.getProcessInstance() != null ? executionEntity.getProcessInstance() : executionEntity);
    identityLinkEntity.setUserId(userId);
    identityLinkEntity.setGroupId(groupId);
    identityLinkEntity.setType(type);
    this.insert(identityLinkEntity);
    return identityLinkEntity;
  }

  @Override
  public IdentityLinkEntity addIdentityLink(TaskEntity taskEntity, String userId, String groupId,
      String type, String outsideId) {
    return null;
  }

  public IdentityLinkEntity addIdentityLink(TaskEntity taskEntity, String userId, String groupId, String type) {
    IdentityLinkEntity identityLinkEntity = (IdentityLinkEntity)this.identityLinkDataManager.create();
    taskEntity.getIdentityLinks().add(identityLinkEntity);
    identityLinkEntity.setTask(taskEntity);
    identityLinkEntity.setUserId(userId);
    identityLinkEntity.setGroupId(groupId);
    identityLinkEntity.setType(type);
    this.insert(identityLinkEntity);
    if (userId != null && taskEntity.getProcessInstanceId() != null) {
      this.involveUser(taskEntity.getProcessInstance(), userId, "participant");
    }

    return identityLinkEntity;
  }

  public IdentityLinkEntity addIdentityLink(ProcessDefinitionEntity processDefinitionEntity, String userId, String groupId) {
    IdentityLinkEntity identityLinkEntity = (IdentityLinkEntity)this.identityLinkDataManager.create();
    processDefinitionEntity.getIdentityLinks().add(identityLinkEntity);
    identityLinkEntity.setProcessDef(processDefinitionEntity);
    identityLinkEntity.setUserId(userId);
    identityLinkEntity.setGroupId(groupId);
    identityLinkEntity.setType("candidate");
    this.insert(identityLinkEntity);
    return identityLinkEntity;
  }

  public IdentityLinkEntity involveUser(ExecutionEntity executionEntity, String userId, String type) {
    Iterator var4 = executionEntity.getIdentityLinks().iterator();

    IdentityLinkEntity identityLink;
    do {
      if (!var4.hasNext()) {
        return this.addIdentityLink((ExecutionEntity)executionEntity, userId, (String)null, type);
      }

      identityLink = (IdentityLinkEntity)var4.next();
    } while(!identityLink.isUser() || !identityLink.getUserId().equals(userId));

    return identityLink;
  }

  public void addCandidateUser(TaskEntity taskEntity, String userId) {
    this.addIdentityLink((TaskEntity)taskEntity, userId, (String)null, "candidate");
  }

  public void addCandidateUsers(TaskEntity taskEntity, Collection<String> candidateUsers) {
    Iterator var3 = candidateUsers.iterator();

    while(var3.hasNext()) {
      String candidateUser = (String)var3.next();
      this.addCandidateUser(taskEntity, candidateUser);
    }

  }

  public void addCandidateGroup(TaskEntity taskEntity, String groupId) {
    this.addIdentityLink((TaskEntity)taskEntity, (String)null, groupId, "candidate");
  }

  public void addCandidateGroups(TaskEntity taskEntity, Collection<String> candidateGroups) {
    Iterator var3 = candidateGroups.iterator();

    while(var3.hasNext()) {
      String candidateGroup = (String)var3.next();
      this.addCandidateGroup(taskEntity, candidateGroup);
    }

  }

  public void addGroupIdentityLink(TaskEntity taskEntity, String groupId, String identityLinkType) {
    this.addIdentityLink((TaskEntity)taskEntity, (String)null, groupId, identityLinkType);
  }

  public void addUserIdentityLink(TaskEntity taskEntity, String userId, String identityLinkType) {
    this.addIdentityLink((TaskEntity)taskEntity, userId, (String)null, identityLinkType);
  }

  public void deleteIdentityLink(ExecutionEntity executionEntity, String userId, String groupId, String type) {
    String id = executionEntity.getProcessInstanceId() != null ? executionEntity.getProcessInstanceId() : executionEntity.getId();
    List<IdentityLinkEntity> identityLinks = this.findIdentityLinkByProcessInstanceUserGroupAndType(id, userId, groupId, type);
    Iterator var7 = identityLinks.iterator();

    while(var7.hasNext()) {
      IdentityLinkEntity identityLink = (IdentityLinkEntity)var7.next();
      this.deleteIdentityLink(identityLink, true);
    }

    executionEntity.getIdentityLinks().removeAll(identityLinks);
  }

  public void deleteIdentityLink(TaskEntity taskEntity, String userId, String groupId, String type) {
    List<IdentityLinkEntity> identityLinks = this.findIdentityLinkByTaskUserGroupAndType(taskEntity.getId(), userId, groupId, type);
    List<String> identityLinkIds = new ArrayList();
    Iterator var7 = identityLinks.iterator();

    while(var7.hasNext()) {
      IdentityLinkEntity identityLink = (IdentityLinkEntity)var7.next();
      this.deleteIdentityLink(identityLink, true);
      identityLinkIds.add(identityLink.getId());
    }

    List<IdentityLinkEntity> removedIdentityLinkEntities = new ArrayList();
    Iterator var11 = taskEntity.getIdentityLinks().iterator();

    while(true) {
      IdentityLinkEntity identityLinkEntity;
      do {
        do {
          do {
            if (!var11.hasNext()) {
              taskEntity.getIdentityLinks().removeAll(removedIdentityLinkEntities);
              return;
            }

            identityLinkEntity = (IdentityLinkEntity)var11.next();
          } while(!"candidate".equals(identityLinkEntity.getType()));
        } while(identityLinkIds.contains(identityLinkEntity.getId()));
      } while((userId == null || !userId.equals(identityLinkEntity.getUserId())) && (groupId == null || !groupId.equals(identityLinkEntity.getGroupId())));

      this.deleteIdentityLink(identityLinkEntity, true);
      removedIdentityLinkEntities.add(identityLinkEntity);
    }
  }

  public void deleteIdentityLink(ProcessDefinitionEntity processDefinitionEntity, String userId, String groupId) {
    List<IdentityLinkEntity> identityLinks = this.findIdentityLinkByProcessDefinitionUserAndGroup(processDefinitionEntity.getId(), userId, groupId);
    Iterator var5 = identityLinks.iterator();

    while(var5.hasNext()) {
      IdentityLinkEntity identityLink = (IdentityLinkEntity)var5.next();
      this.deleteIdentityLink(identityLink, false);
    }

  }

  public void deleteIdentityLinksByTaskId(String taskId) {
    List<IdentityLinkEntity> identityLinks = this.findIdentityLinksByTaskId(taskId);
    Iterator var3 = identityLinks.iterator();

    while(var3.hasNext()) {
      IdentityLinkEntity identityLink = (IdentityLinkEntity)var3.next();
      this.deleteIdentityLink(identityLink, false);
    }

  }

  public void deleteIdentityLinksByProcDef(String processDefId) {
    this.identityLinkDataManager.deleteIdentityLinksByProcDef(processDefId);
  }

  public IdentityLinkDataManager getIdentityLinkDataManager() {
    return this.identityLinkDataManager;
  }

  public void setIdentityLinkDataManager(IdentityLinkDataManager identityLinkDataManager) {
    this.identityLinkDataManager = identityLinkDataManager;
  }
}
